
#include "../include/qtwi/ithread.hpp"


void IThread::start()
{
	data.setRunning(true);
	if(pthread_create(&thread, NULL, &IThread::generic_ithread_func, &data) != 0)
		throw IThreadCreationError();

	pthread_detach(thread);
}

void IThread::stop()
{
	data.setRunning(false);
}

void IThread::join()
{
	if(pthread_join(thread, NULL) != 0)
		throw IThreadUnjoinable();
}

void* IThread::generic_ithread_func(void *ithread_data)
{
	IThreadData *d = static_cast<IThreadData*>(ithread_data);
	d->getIThread().action(**d);
	
	// TODO: allow handler to return user data
	
	return NULL;
}
